<?php

namespace app\admin\command;

use app\admin\command\Api\library\Builder;


use app\admin\model\SpiderAiqiyi;
use app\admin\model\SpiderAiqiyiCollection;
use app\admin\model\SpiderAiqiyiLog;
use app\admin\model\SpiderYouku;
use app\admin\model\SpiderYoukuCollection;
use app\admin\model\SpiderYoukuLog;
use phpspider\core\phpspider;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Model;

class Spider1 extends Command
{
    protected function configure()
    {
        /* Do NOT delete this comment */
        /* 不要删除这段注释 */
        $this
            ->setName('spider1')  //优酷视频抓包
            ->addOption('type', 't', Option::VALUE_REQUIRED, 'type', null)
            ->setDescription("spider 抓包");

    }

    protected function execute(Input $input, Output $output)
    {
        $type = $input->getOption('type');
        //初始更新
        if(empty($type)){
            $list = SpiderYouku::select();
            if(empty($list)) return true;
            try{
                foreach ($list as $val){
                    $url = $val["url"];
                    $parent_id = $val["id"];
                    if(empty($url)) continue;
                    if(strpos($url,"?")!==false){
                        $url.="&userId=".time();
                    }else{
                        $url.="?userId=".time();
                    }
                    $cookie = $val["cookie"];
                    $cookie = $cookie?:"QLJ6Gln1CgICAasPPVMW2NuC";
                    requests::set_cookie("cna", $cookie);
                    $content = requests::get($url);
                    $hot = selector::select($content, "//span[@class='video-heat-text']");//热度
                    $update_date = selector::select($content, "//div[@class='title-info']//span");//更新日期
                    $arr = selector::select($content, "//a[@class='box-item']");//集数
                    if(empty($arr) || !is_array($arr)){
                        echo "集数数组请求失败 id:".$val["id"].PHP_EOL;
                        dump($arr);
                       // return false;
                    }
                    $arr = $arr?:[];
                    $jishu = 0;  //最新集数
                    $brr_ar = [];
                    foreach ($arr as $arr_val){
                        $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
                        $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
                        if($a<1 && $b != "预告"){
                            $a = $jishu + 1;
                            $jishu = $a;
                        }else{
                            if($a>0 && $jishu<$a && $b != "预告"){
                                $jishu = $a;
                            }
                        }
                        $brr_ar[$a]=$b;
                    }

                    $ar = selector::select($content, "//div[@class='anthology-content']");//集数
                    //评论数接口有加密验证，无法抓包
                    $ar_content = $ar[0]??"";
                    //$br = selector::select($ar, "#<a href=\"(.+?)\" data-spm=\"(.+?)\" .+?</a>#","regex");//集数
                    $br = selector::select($ar_content, "#<a\s+href=\"(.+?)\"\s+data-spm=\"(.+?)\"\s+.+?</a>#","regex");//集数
                    if($br && is_array($br) && isset($br[0]) && isset($br[1]) && is_array($br[0]) && is_array($br[1])){
                        foreach ($br[0] as $br0_k=>$br0_val){
                            if(isset($br[1][$br0_k])){
                                $url = $br0_val;
                                $br1_val = $br[1][$br0_k];
                                $br1_val_ar = explode("_",$br1_val);
                                $collection_number = (int)($br1_val_ar[2]??0);
                                $model = SpiderYoukuCollection::where("parent_id",$parent_id)->where("collection_number",$collection_number)->find();

                                $collection_desc = $brr_ar[$collection_number]??"";
                                $data = [
                                    "parent_id"=>$parent_id,
                                    "url"=>$url,
                                    "collection_number"=>$collection_number,
                                    "collection_desc"=>$collection_desc
                                ];
                                if(empty($model)){
                                    $model = new SpiderYoukuCollection();
                                    $model->save($data);
                                }
                            }
                        }
                    }

                    $update_data = [
                        "hot"=>(string)$hot,
                        "hot_number"=>$hot,
                        "max_hot_number"=>$hot,
                        "max_hot"=>(string)$hot,
                        "update_date"=>$update_date,
                        "comment_number"=>0,
                        "barrage_number"=>0,
                        "current_collection"=>$jishu
                    ];
                    SpiderYouku::update($update_data,['id'=>$parent_id]);
                }
            }catch (\Exception $e){
                throw $e;
            }
            return true;
        }



        //更新集的热度，评论数，弹幕数 一小时一次
        if($type=="date"){
            $list = SpiderYouku::select();
            if(empty($list)){
                echo "list 为空 ".PHP_EOL;
                return false;
            }
            try{
                foreach ($list as $val){
                    $url = $val["url"];
                    $parent_id = $val["id"];
                    if(empty($url)) continue;
                    if(strpos($url,"?")!==false){
                        $url."=&userId=".time();
                    }else{
                        $url.="?userId=".time();
                    }
                    $cookie = $val["cookie"];
                    $cookie = $cookie?:"QLJ6Gln1CgICAasPPVMW2NuC";
                    requests::set_cookie("cna",$cookie);
                    $content = requests::get($url);
                    $hot = selector::select($content, "//span[@class='video-heat-text']");//热度
                    $update_date = selector::select($content, "//div[@class='title-info']//span");//更新日期
                    $arr = selector::select($content, "//a[@class='box-item']");//集数
                    $jishu = 0;  //最新集数
                    if(empty($arr) || !is_array($arr)){
                        echo "集数数组请求失败 id:".$val['id'].PHP_EOL;
//                        return false;
                    }
                    $arr = $arr?:[];
                    foreach ($arr as $arr_val){
                        $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
                        $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
                        if($a<1 && $b != "预告"){
                            $a = $jishu + 1;
                            $jishu = $a;
                        }else{
                            if($a>0 && $jishu<$a && $b != "预告"){
                                $jishu = $a;
                            }
                        }
                    }
                    $max_hot_number = $val["max_hot_number"]>$hot?$val["max_hot_number"]:$hot;

                    $update_data = [
                        "hot"=>(string)$hot,
                        "hot_number"=>(int)$hot,
                        "max_hot_number"=>(int)$max_hot_number,
                        "max_hot"=>(string)$max_hot_number,
                        "update_date"=>$update_date?:"",
                        "comment_number"=>0,
                        "barrage_number"=>0,
                        "current_collection"=>$jishu?:""
                    ];
                    SpiderYouku::update($update_data,['id'=>$parent_id]);

                    //日志
                    $date = date("Y-m-d");
                    $model = SpiderYoukuLog::where("parent_id",$parent_id)->where("date",$date)->find();
                    if(empty($model)){
                        $model = new SpiderYoukuLog();
                    }
                    $log=[
                        "hot_number"=>(int)$hot,
                        "comment_number"=>0,
                        "barrage_number"=>0,
                        "play_number"=>0,
                        "date"=>$date,
                        "parent_id"=>$parent_id,
                        "createdtime"=>time()
                    ];
                    $model->save($log);
                }
            }catch (\Exception $e){
                throw $e;
            }
            return true;
        }
    }

}
